import AppLayout from '@/layout/AppLayout.vue';
import { useAdminStore } from '@/store/admin';
import { useUserStore } from '@/store/user';
import { createRouter, createWebHistory } from 'vue-router';

const title = '小米爱编程';
const adminDefaultRoutePath = '/backend';
const whiteRoute = ['case_show', 'news_show', 'pro_show', 'page', 'about', 'news', 'products', 'cases', 'admin_login', 'user_login', 'mineDoc', 'interfaceList', 'interfaceCode', 'signature', 'home'];

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('@/views/Home.vue')
        },
        {
            path: '/backend',
            component: AppLayout,
            children: [
                {
                    path: '/backend',
                    name: 'backend_dashboard',
                    component: () => import('@/views/backend/Dashboard.vue')
                },
                {
                    path: '/backend/other/upload.html',
                    name: 'backend_other_upload',
                    component: () => import('@/views/backend/other/Upload.vue')
                },
                {
                    path: '/backend/other/image.html',
                    name: 'backend_other_image',
                    component: () => import('@/views/backend/other/Image.vue')
                },
                {
                    path: '/backend/config/index.html',
                    name: 'backend_config_index',
                    component: () => import('@/views/backend/config/Index.vue')
                },
                {
                    path: '/backend/config/api.html',
                    name: 'backend_config_api',
                    component: () => import('@/views/backend/config/Api.vue')
                },
                {
                    path: '/backend/extend/ad/index.html',
                    name: 'backend_ad',
                    component: () => import('@/views/backend/extend/ad/Index.vue')
                },
                {
                    path: '/backend/category/index.html',
                    name: 'backend_category',
                    component: () => import('@/views/backend/category/Index.vue')
                },
                {
                    path: '/backend/catefield/index.html',
                    name: 'backend_catefield_index',
                    component: () => import('@/views/backend/category/catefield/Index.vue')
                },
                {
                    path: '/backend/extend/index.html',
                    name: 'backend_extend_index',
                    component: () => import('@/views/backend/category/extend/Index.vue')
                },
                {
                    path: '/backend/model/index.html',
                    name: 'backend_model_index',
                    component: () => import('@/views/backend/category/model/Index.vue')
                },
                {
                    path: '/backend/form/index.html',
                    name: 'backend_form_index',
                    component: () => import('@/views/backend/category/form/Index.vue')
                },
                {
                    path: '/backend/content/index.html',
                    name: 'backend_content_index',
                    component: () => import('@/views/backend/content/Index.vue'),
                    children: [
                        {
                            path: '/backend/content/news/list.html',
                            name: 'backend_content_news_list',
                            component: () => import('@/views/backend/content/news/List.vue')
                        },
                        {
                            path: '/backend/content/pro/list.html',
                            name: 'backend_content_pro_list',
                            component: () => import('@/views/backend/content/pro/List.vue')
                        },
                        {
                            path: '/backend/content/case/list.html',
                            name: 'backend_content_case_list',
                            component: () => import('@/views/backend/content/case/List.vue')
                        },
                        {
                            path: '/backend/content/job/list.html',
                            name: 'backend_content_job_list',
                            component: () => import('@/views/backend/content/job/List.vue')
                        },
                        {
                            path: '/backend/content/page.html',
                            name: 'backend_content_page',
                            component: () => import('@/views/backend/content/Page.vue')
                        }
                    ]
                },
                {
                    path: '/backend/content/recycle.html',
                    name: 'backend_content_recycle',
                    component: () => import('@/views/backend/content/Recycle.vue')
                },
                {
                    path: '/backend/content/block/index.html',
                    name: 'backend_block_index',
                    component: () => import('@/views/backend/content/block/Index.vue')
                },
                {
                    path: '/backend/user/index.html',
                    name: 'backend_user_index',
                    component: () => import('@/views/backend/user/Index.vue')
                }
            ]
        },
        {
            path: '/portal/page/:id',
            name: 'page',
            component: () => import('@/views/content/page/Index.vue'),
            props: true
        },
        {
            path: '/portal/news/:id',
            name: 'news',
            component: () => import('@/views/content/news/Index.vue'),
            props: true
        },
        {
            path: '/portal/news/show/:id',
            name: 'news_show',
            component: () => import('@/views/content/news/Show.vue'),
            props: true
        },
        {
            path: '/portal/pro/:id',
            name: 'products',
            component: () => import('@/views/content/pro/Index.vue'),
            props: true
        },
        {
            path: '/portal/pro/show/:id',
            name: 'pro_show',
            component: () => import('@/views/content/pro/Show.vue'),
            props: true
        },
        {
            path: '/about',
            name: 'about',
            component: () => import('@/views/content/page/About.vue')
        },
        // {
        //     path: '/news',
        //     name: 'news',
        //     component: () => import('@/views/content/news/Index.vue')
        // },
        // {
        //     path: '/products',
        //     name: 'products',
        //     component: () => import('@/views/content/pro/Index.vue')
        // },
        {
            path: '/cases',
            name: 'cases',
            component: () => import('@/views/content/cases/Index.vue')
        },
        {
            path: '/cases/show/:id',
            name: 'case_show',
            component: () => import('@/views/content/cases/Show.vue')
        },
        {
            path: '/pages/notfound',
            name: 'notfound',
            component: () => import('@/views/pages/NotFound.vue')
        },
        {
            path: '/backend/login',
            name: 'admin_login',
            component: () => import('@/views/backend/Login.vue')
        },
        {
            path: '/user/login',
            name: 'user_login',
            component: () => import('@/views/user/Login.vue')
        },
        {
            path: '/auth/access',
            name: 'accessDenied',
            component: () => import('@/views/pages/auth/Access.vue')
        },
        {
            path: '/auth/error',
            name: 'error',
            component: () => import('@/views/pages/auth/Error.vue')
        }
    ]
});

router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore();
    const adminStore = useAdminStore();
    let toTitle = to.meta.title ? to.meta.title : to.name;
    document.title = `${toTitle} - ${title}`;

    const user_token = userStore.token;
    const admin_token = adminStore.token;

    if (!whiteRoute.includes(to.name)) {
        if (to.fullPath.startsWith('/backend')) {
            if (admin_token) {
                if (!adminStore.adminInfo && adminStore.adminInfo == undefined) {
                    const data = await adminStore.requestAdminInfo();
                    if (to.name == 'admin_login') {
                        next({ name: adminDefaultRoutePath });
                    } else {
                        data && next({ path: to.path, query: to.query });
                    }
                } else {
                    if (to.name == 'admin_login') {
                        next({ name: adminDefaultRoutePath });
                    } else {
                        next();
                    }
                }
            } else {
                next({ name: 'admin_login', query: { redirect: to.fullPath } });
            }
        }
        if (to.fullPath.startsWith('/user')) {
            if (user_token) {
                if (!userStore.userInfo && userStore.userInfo == undefined) {
                    const data = await userStore.requestUserInfo();
                    data && next({ path: to.path, query: to.query });
                } else {
                    next();
                }
            } else {
                next({ name: 'user_login', query: { redirect: to.fullPath } });
            }
        }
    } else {
        if (to.name == 'admin_login') {
            if (admin_token) {
                if (!adminStore.adminInfo && adminStore.adminInfo == undefined) {
                    const data = await adminStore.requestAdminInfo();
                    console.log(to.name);
                    if (to.name == 'admin_login') {
                        next({ name: adminDefaultRoutePath });
                    } else {
                        data && next({ path: to.path, query: to.query });
                    }
                } else {
                    if (to.name == 'admin_login') {
                        next({ path: adminDefaultRoutePath });
                    } else {
                        next();
                    }
                }
            } else {
                next();
            }
        } else {
            next();
        }
    }
});

router.afterEach((to, from) => {
    // 跳转到404页面
    if (to.name === 'notfound') {
        router.push({ name: 'notfound' });
    }
});

router.onError((error) => {
    console.log(error);
});
export default router;
